package androidServlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.tecnick.htmlutils.htmlentities.HTMLEntities;

import sessionMenegment.SessionMgr;

import controller.Controller;
import dataBase.Business;
import dataBase.Order;

@SuppressWarnings("serial")
public class ClientOneOrderAndroid extends HttpServlet {

	private SimpleDateFormat formatter;
	private static String NEW_LINE_MARK = "!@#%";
	
	public ClientOneOrderAndroid() {
		formatter = new SimpleDateFormat("dd-MM-yy");
		formatter.setTimeZone(TimeZone.getTimeZone("Israel"));
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {

		PrintWriter out = resp.getWriter();

		try{
			//biz(User)Name , orderNum, phone - from QR
			//bizID , orderNum, phone - find order
			//orderKey, phone - for one order
			String bizUserName = req.getParameter("bizName");
			String bizKey= req.getParameter("bizKey");
			String phone = req.getParameter("phone");
			String orderKey = req.getParameter("orderKey");
			//String bizUserName = Controller.getInstance().bizNameToUserName(bizName);
						
			//if we get biz key convert it to biz user name
			if (bizKey!=null)
			{
				long bizIdLong = Long.parseLong(bizKey);
				Business b = Controller.getInstance().keyToBusiness(bizIdLong);
				if (b!=null)
				{
					bizUserName = b.getUserName();
				}	
			}
			
			//get the order
			Order order = null;
			
			//if not from QR - need to get the username by the key
			if (orderKey!=null)
			{	
				long orderIdLong = Long.parseLong(orderKey);
				order = Controller.getInstance().keyToOrder(orderIdLong);
			}
			else
			{
				int orderNum = Integer.parseInt(req.getParameter("orderNum"));
				order = Controller.getInstance().getOrder(orderNum, bizUserName);
			}
				
			System.out.println("order "+order);
			
			System.out.println("order phone "+order.getPhoneNumber().getNumber());
			System.out.println(phone);
			
			if (order!=null && order.getPhoneNumber().getNumber().equals(phone))
			{
				List<String> statuses = order.getStatusNamesList();
				String currentStatus = Controller.getInstance().getCurrentStatusOfOrder(order);
				int currentStatusPosition = statuses.indexOf(currentStatus);

				boolean first = true;
				out.println("{\"statuses\": [");

				for (String status: statuses)
				{
					if (first)
						first = false;
					else
						out.println(",");

					out.println("  {");
					out.print("    \"statusName\": ");
					out.print("\""+status+"\"");
					out.println("  }");

				}

				out.println("],");

				out.println("\"currentStatusPosition\":");
				out.print("\""+Math.max(currentStatusPosition,0)+"\"");
				out.println(",");
				
				out.println("\"from\":");
				out.print("\""+Controller.getInstance().userNameToBizName(order.getBusinessUserName())+"\"");
				out.println(",");

				Business b = Controller.getInstance().getBiz(order.getBusinessUserName());
				if (b!=null)
				{
					out.println("\"bizKey\":");
					out.print("\""+b.getKey().getId()+"\"");
					out.println(",");
			    }
			    
				out.println("\"orderNum\":");
				out.print("\""+order.getOrderNum()+"\"");
				out.println(",");
				
				out.println("\"details\":");
				out.print("\""+order.getDetails()+"\"");
				out.println(",");
				
				out.println("\"destAddress\":");
				out.print("\""+order.getDestAdd()+"\"");
				out.println(",");
				
				out.println("\"date\":");
				out.print("\""+formatter.format(order.getStartTime())+"\"");
				out.println(",");
				
				out.println("\"messages\":");
				if (order.getMessages()==null)
					out.print("\"\"");
				else
					out.print("\""+order.getMessages().getValue().replaceAll("\n", NEW_LINE_MARK)+"\"");
				
				
//				String[] messagesArray = order.getMessages().getValue().split("\n");
//				
//				for (int i=0; i<str.length; i++)
//					System.out.println("###"+str[i]);
				
				out.println("}");
			}
			else
			{
				out.println("false");				
			}

		}
		catch(NumberFormatException e)
		{
			System.out.println("NumberFormatException");
			out.println("false");

		}
		out.flush();


	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {

	}

}
